<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
  <head>
    <title>添加物业费记录-物业费交款</title>
  </head>
  <body>
    <script type="text/javascript">
      Ext.onReady(function(){
        Ext.QuickTips.init();
        Ext.BLANK_IMAGE_URL = '/javascripts/ext/resources/images/default/s.gif';
        //设置对话框最大宽度
        Ext.Msg.getDialog().setWidth(300);

        var propertyLastYear = parseInt(<%=@house.property_pay_year%>);
        var propertyLastMonth = parseInt(<%=@house.property_pay_month%>) ;

        
        var houseId = new Ext.form.Hidden({
          name: 'house_id',
          value: '<%=@house.id%>'
        });

        var ownerNumber = new Ext.form.TextField({
          fieldLabel: '业主编号',
          allowBlank: false,
          name: 'number',
          anchor: '95%',
          readOnly:true,
          value:'<%=@house.number%>'
        });
        var owner = new Ext.form.TextField({
          fieldLabel: '业主姓名',
          allowBlank: false,
          name: 'owner',
          anchor: '95%',
          readOnly:true,
          value:'<%=@house.owner%>'
        });
        var propertyLastDate = new Ext.form.TextField({
          fieldLabel: '交费结止',
          allowBlank: false,
          name: 'owner',
          anchor: '95%',
          readOnly:true,
          value:'<%=@house.property_pay_year%>年<%=@house.property_pay_month%>月'
        });

        var area =  new Ext.form.TextField({
          fieldLabel: '面积',
          allowBlank: false,
          readOnly: true,
          name: 'area',
          anchor: '95%',
          value:'<%=@house.area%>'
        });

        //本次金额
        var propertyAmount =  new Ext.form.NumberField({
          fieldLabel: '本次金额',
          allowBlank: false,
          name: 'amount',
          anchor: '95%',
          readOnly:true
        });

        var propertyCurrentYear = new Ext.form.ComboBox({ //combobox
          name: 'current_year',
          fieldLabel: '本次交费年',
          width: '90',
          resiable: true,
          typeAhead: true,
          allowBlank: false,
          handleHeight: 100, //
          displayField: 'key',
          valueField: 'value',
          mode: 'local',
          selectOnFocus: true,
          triggerAction: 'all',//这个属性非常重要,可以在选择一个值以后,依然还显示其它的值.
          readOnly: true,
          emptyText: '选择年...',
          store: new Ext.data.Store({
            data: [['2004', '2004'],['2005', '2005'],['2006', '2006'],['2007', '2007'],['2008', '2008'], ['2009', '2009'], ['2010', '2010'], ['2011', '2011'], ['2012', '2012']],
            reader: new Ext.data.ArrayReader({
              id: 0
            }, [{
                name: 'value'
              }, {
                name: 'key'
              }])
          }), //数据储存到这个comboBox
          anchor: '95%'
        });
        propertyCurrentYear.setValue(new Date().clearTime(true).getFullYear());
        propertyCurrentYear.on('select',countPropertyFee);
        
        var propertyCurrentMonth = new Ext.form.ComboBox({ //combobox
          name: 'current_month',
          fieldLabel: '月',
          width: '90',
          resiable: true,
          typeAhead: true,
          allowBlank: false,
          handleHeight: 100, //
          displayField: 'key',
          valueField: 'value',
          mode: 'local',
          selectOnFocus: true,
          triggerAction: 'all',//这个属性非常重要,可以在选择一个值以后,依然还显示其它的值.
          readOnly: true,
          emptyText: '选择月...',
          store: new Ext.data.Store({
            data: [['1', '1'], ['2', '2'], ['3', '3'], ['4', '4'], ['5', '5'], ['6', '6'],['7', '7'], ['8', '8'], ['9', '9'], ['10', '10'], ['11', '11'], ['12', '12']],
            reader: new Ext.data.ArrayReader({
              id: 0
            }, [{
                name: 'value'
              }, {
                name: 'key'
              }])
          }), //数据储存到这个comboBox
          anchor: '95%'
        });
        var tempMonth = <%=@house.property_pay_month%>;
        if(tempMonth%3==1){
          tempMonth=tempMonth-1+3;
        }else{
          tempMonth=tempMonth+3;
        }
        if(tempMonth > 12 ){
          tempMonth = 3;
          propertyCurrentYear.setValue(propertyCurrentYear.getValue()+1);
        }
        propertyCurrentMonth.setValue(tempMonth);
        propertyCurrentMonth.on('select',countPropertyFee);

        //根据客户要求，年份可以自己选择-------------------
        /*var yearTypeStore = new Ext.data.ArrayStore({
          //fields: ['value', 'text'],
          fields: ['text'],
          data : yearArray
        });

        var year = new Ext.form.ComboBox({
          fieldLabel: '本次交费年',
          store: yearTypeStore,
          valueField:'text',
          displayField:'text',
          typeAhead: true,
          mode: 'local',
          triggerAction: 'all',
          selectOnFocus:true,
          name:'current_year',
          emptyText:'请选择年...',
          allowBlank: false,
          anchor: '95%'
        });*/

        //根据客户要求，月份可以自己选择
        var monthTypeStore = new Ext.data.ArrayStore({
          //fields: ['value', 'text'],
          fields: ['text'],
          data : monthArray
        });

        var month = new Ext.form.ComboBox({
          fieldLabel: '月',
          store: monthTypeStore,
          valueField:'text',
          displayField:'text',
          typeAhead: true,
          mode: 'local',
          triggerAction: 'all',
          selectOnFocus:true,
          name:'current_month',
          emptyText:'请选择月份...',
          allowBlank: false,
          anchor: '95%'
        });

        month.on('select', function(combo, record, index) {
          countPropertyFee();
        })
        
        //本次单价
        var buildingType = "<%=@house.building.sub_type%>";
        var unitPrice = 0;
        if(buildingType=="多层"){
          unitPrice = <%=session[:multi_property_unit_price]%>;
        }else{
          unitPrice = <%=session[:high_property_unit_price]%>;
        }
        
        var propertyUnitPrice =  new Ext.form.NumberField({
          fieldLabel: '本次单价',
          allowBlank: false,
          name: 'unit_price',
          anchor: '95%',
          readOnly:true,
          value: unitPrice
        });
        
        //提交按钮
        var addPropertyFeeButton = new Ext.Button({
          text: '提交',
          iconCls: 'icon-save',
          handler: function(){
            addPropertyFeeButton.setDisabled(true);
            if (newPropertyRecordFormPanel.form.isValid()) {
              if(propertyAmount.getValue() == 0){
                Ext.MessageBox.show({
                  title: '提示消息',
                  msg: '本次资费金额为0，不能提交!',
                  buttons: Ext.MessageBox.OK,
                  icon: Ext.MessageBox.INFO
                });
                addPropertyFeeButton.enable();
                return;
              }else if(propertyCurrentYear.getValue() < propertyLastYear){
                Ext.MessageBox.show({
                  title: '提示消息',
                  msg: '本次结止年份小于上次结止日期！有误，请核实!',
                  buttons: Ext.MessageBox.OK,
                  icon: Ext.MessageBox.INFO
                });
                addPropertyFeeButton.enable();
                return;
              }else if(propertyCurrentYear.getValue() == propertyLastYear){
                if(propertyCurrentMonth.getValue() <= propertyLastMonth){
                  Ext.MessageBox.show({
                    title: '提示消息',
                    msg: '本次结止月份小于上次结止日期！有误，请核实!',
                    buttons: Ext.MessageBox.OK,
                    icon: Ext.MessageBox.INFO
                  });
                  addPropertyFeeButton.enable();
                  return;
                }
              }

              newPropertyRecordFormPanel.form.submit({
                success: function(result, resp){
                  if (resp.result.info.indexOf("成功") >= 0) {
                    newPropertyRecordWin.destroy();
                  } else {
                    Ext.MessageBox.alert('消息', resp.result.info);
                    addPropertyFeeButton.enable();
                  }
                },
                failure: function(result, request){
                  addPropertyFeeButton.enable();
                  Ext.MessageBox.alert('消息', '服务器的通信失败!');
                }
              });
            }
            else {
              addPropertyFeeButton.enable();
              //Ext.MessageBox.alert('输入验证有错', '请输入必输选项');
            }
          }
        })
        
        var newPropertyRecordFormPanel = new Ext.form.FormPanel({
          //baseCls: 'x-plain',
          labelWidth: 65,
          width: '400',
          labelAlign: 'right',
          frame : true,
          url: '/fee/createPropertyRecord',
          //defaultType: 'textfield',
          items: [{
              layout: 'column',
              border: false,
              items: [{
                  columnWidth: .5,
                  layout: 'form',
                  border: false,
                  items: [ownerNumber]
                }, {
                  columnWidth: .5,
                  layout: 'form',
                  border: false,
                  items: [owner]
                }]
            },{
              layout: 'column',
              border: false,
              items: [{
                  columnWidth: .5,
                  layout: 'form',
                  border: false,
                  items: [propertyLastDate]
                }, {
                  columnWidth: .5,
                  layout: 'form',
                  border: false,
                  items: [area]
                }]
            },{
              layout: 'column',
              border: false,
              items: [{
                  columnWidth: .5,
                  layout: 'form',
                  border: false,
                  items: [propertyCurrentYear]
                }, {
                  columnWidth: .5,
                  layout: 'form',
                  border: false,
                  items: [month]
                }]
            },{
              layout: 'column',
              border: false,
              items: [{
                  columnWidth: .5,
                  layout: 'form',
                  border: false,
                  items: [propertyUnitPrice]
                }, {
                  columnWidth: .5,
                  layout: 'form',
                  border: false,
                  items: [propertyAmount]
                }]
            },houseId],
          buttons: [addPropertyFeeButton,{
              text: '关闭',
              iconCls: 'exit',
              handler: function(){
                newPropertyRecordWin.destroy();
              }
            }]
        });
        newPropertyRecordFormPanel.render('new_property_form');
        function countPropertyFee(){
          //本次选择的年月
          var cYear = parseInt(propertyCurrentYear.getValue());
          var cMonth = parseInt(month.getValue());
          //上次交费的年月
          var lYear = propertyLastYear;
          var lMonth = propertyLastMonth;
          var areaNumber = area.getValue();
          if(cYear > lYear){
            //propertyAmount.setValue(Math.round(months*unitPrice*10*areaNumber*100/100)/10);
            var months = (cYear-1-lYear)*12 + (12-lMonth) + cMonth;
            var amount = months*unitPrice*areaNumber;            
            propertyAmount.setValue(amount.toFixed(2));
          }else if(cYear == lYear){
            if(cMonth>lMonth){
              //propertyAmount.setValue(Math.round((cMonth - lMonth)*unitPrice*10*areaNumber*100/100)/10);
              var amount = (cMonth - lMonth)*unitPrice*areaNumber;              
              propertyAmount.setValue(amount.toFixed(2));
            }else if(cMonth == lMonth){
              propertyAmount.setValue(0);
            }
          }
        };
        countPropertyFee();

        //为了新需求计算方便，在这儿给本次金额设置为0
        propertyAmount.setValue(0);
      })
    </script>
    <div id="new_property_form"></div>
  </body>
</html>
